var express = require('express');
const { response } = require('../app');
var router = express.Router();
//引入业务层
let studentService = require('../src/service/studentService');


//使用rest风格
router.get("/getstudentbypage", async(req,res)=>{

   console.log(req.query,"=====>>");
  //获得客户端的传入的参数
  let {curpage,pagesize} = req.query;
  //console.log(curpage,pagesize)
  let result =await studentService.getStudentInfoByPage(curpage,pagesize);
  res.json({code:0,...result});

})

//添加学生
router.post("/addstudent",async (req,res)=>{
   //获得客户端的参数
   let {name,age,gender,address}=req.body;
   //添加学生
   let result =  await studentService.addStudent({name,age,gender,address});

    if(result){
      res.json({code:0,msg:"添加成功"})
    }else{
      res.json({code:1,msg:"添加失败"})
    }
})

//删除学生
router.delete("/deletestudent/:stuid",async (req,res)=>{
  //获得客户端的参数
  let {stuid}=req.params;
  //删除学生
  let result =  await studentService.deleteStudent(stuid);
  if(result.deletedCount!=0){
    res.json({code:0,msg:"删除成功"})
  }else{
    res.json({code:1,msg:"删除失败"})
  }
});

//修改学生信息
router.put("/updatestudentinfo",async(req,res)=>{
    //获得客户端的参数
    let {stuid,name,age,gender,address} = req.body;
     //修改数据
     let result = await  studentService.updateStudentInfo(stuid,{name,age,gender,address})
      if(result){
        res.json({code:0,msg:"修改成功"})
      }else{
        res.json({code:1,msg:"修改失败"})
      }
});

/**
 * 查询学生详细信息
 */
router.get("/showDeailInfo/:stuid", async(req,res)=>{

  //获得客户端的参数
  let {stuid} = req.params;
  //查询详细信息
  let  result = await studentService.showStudentDetailInfo(stuid);
  //console.log(result)
  res.json(result);
})

module.exports = router;
